package com.star.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.star.constant.MessageConstant;
import com.star.constant.StatusConstant;
import com.star.context.BaseContext;
import com.star.dto.SetmealDTO;
import com.star.dto.SetmealPageQueryDTO;
import com.star.entity.Category;
import com.star.entity.Setmeal;
import com.star.entity.SetmealDish;
import com.star.exception.BaseException;
import com.star.exception.DeletionNotAllowedException;
import com.star.mapper.CategoryMapper;
import com.star.mapper.SetmealDishMapper;
import com.star.mapper.SetmealMapper;
import com.star.result.PageResult;
import com.star.service.SetmealService;
import com.star.vo.DishItemVO;
import com.star.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {

        // 添加基本数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 默认停售状态
        setmeal.setStatus(StatusConstant.DISABLE);
        // 默认商家id
        setmeal.setBusinessId(BaseContext.getCurrentId());
        setmealMapper.insert(setmeal);
        // 获取插入指向后的主键值
        Long setmealId = setmeal.getId();
        // 添加菜品数据
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if (setmealDishList != null && !setmealDishList.isEmpty()) {
            setmealDishList.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            // 批量插入菜品数据
            setmealDishMapper.insertBatch(setmealDishList);
        }
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealVO getById(Long id) {

        SetmealVO setmealVO = new SetmealVO();
        // 查询基本数据
        Setmeal setmeal = setmealMapper.getById(id);
        if(setmeal == null){
            throw new BaseException("套餐不存在");
        }
        BeanUtils.copyProperties(setmeal, setmealVO);
        // 查询套餐菜品数据
        List<SetmealDish> setmealDishList = setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 修改套餐和对应的菜品数据
     * @param setmealDTO
     */
    @Transactional
    public void updateWithDishes(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 修改套餐表基本数据
        setmealMapper.update(setmeal);
        // 修改对应的菜品数据 先删除再修改
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if (setmealDishList != null && !setmealDishList.isEmpty()) {
            setmealDishList.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            // 批量插入菜品数据
            setmealDishMapper.insertBatch(setmealDishList);
        }
    }

    /**
     * 批量删除套餐和对应的菜品数据
     * @param ids
     */
    @Transactional
    public void deleteWithDishes(List<Long> ids) {
        for (Long id: ids){
            Setmeal setmeal = setmealMapper.getById(id);
            if(Objects.equals(setmeal.getStatus(), StatusConstant.ENABLE)){
                // 启售中的套餐不删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        // 根据套餐id集合批量删除套餐数据
        setmealMapper.deleteBySetmealIds(ids);
        // 根据套餐id集合批量删除菜品数据
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     */
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    /**
     * 根据setmeal查询套餐列表
     * @param setmeal
     * @return
     */
    public List<Setmeal> getBySetmeal(Setmeal setmeal) {
        return setmealMapper.getBySetmeal(setmeal);
    }

    /**
     * 根据套餐id查询套餐信息以及包含的菜品
     * @param id
     * @return
     */
    public SetmealVO getSetmealDishById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        // 套餐分类名称
        Category category = categoryMapper.getById(setmeal.getCategoryId());
        String categoryName = category.getName();
        List<SetmealDish> setmealDishList = setmealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);
        setmealVO.setCategoryName(categoryName);
        return setmealVO;
    }
}
